(function() {
    angular.module('app').factory('brandControllerComponentService', brandControllerComponentService);

    brandControllerComponentService.$inject = ['util', '$q', 'atomicNotifyService','$rootScope'];

    function brandControllerComponentService(util, $q, atomicNotifyService,$rootScope) {

        var basicInfoShowning = -1;
        
        return {
            get: _get,

            getCollection: _getCollection,

            delCollection: _delCollection,

            getBrandBasic:_getBrandBasic,

            brandBasicInit:_brandBasicInit
        }

        function _get(json) {

            return util.$get({
                url: '/marketconditions/brand/brandBySearch.htm',
                data: json
            });
        }

        function _getCollection() {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/getCollection.htm',
                data: {
                    type: 'brand'
                }
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);

            });

            return defer.promise;
        }

        function _delCollection(brand) {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/deleteCollections.htm',
                data: {
                    targetId: brand.brandName,
                    type: 'brand'
                }
            }).then(function(resp) {

                brand.collectionStatus = null;
                var message = '取消监控' + brand.brandName;
                atomicNotifyService.info(message);

                defer.resolve();

            }, function(err) {

                var message = '取消监控' + brand.brandName + '失败!';
                atomicNotifyService.warning(message);

                defer.reject();
            });

            return defer.promise;
        }

        function _getBrandBasic(e, index) {
            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { brandname: vm.table.brands[index].brandName });
                return;
            }

            if (basicInfoShowning == index) {
                vm.table.brands.splice(index + 1, 1);
                basicInfoShowning = -1;
                return;
            }

            // 清空之前的数据
            if (basicInfoShowning >= 0) {
                vm.table.brands.splice(basicInfoShowning + 1, 1);
            }

            if (basicInfoShowning >= 0 && index > basicInfoShowning) {
                index--;
            }

            vm.table.brands.splice(index + 1, 0, {
                brandName: null,
                brandname: vm.table.brands[index].brandName
            });
            // console.log({
            //     brandName: null,
            //     brandname: vm.table.brands[index].brandName
            // });
            basicInfoShowning = index;
        }

        function _brandBasicInit(){
            basicInfoShowning = -1;
        }
    }
})();
